package com.xm.xmapp;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.adapter.AccountRecordAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ListViewOnclickInterFace;
import com.xm.xmapp.request.bean.Get_user_account_recordRequest;
import com.xm.xmapp.response.bean.Get_user_account_recordResponse;
import com.xm.xmapp.response.bean.ResponseAccountRecord;

public class AccountRecordListActivity extends BaseActivity {
	private Get_user_account_recordRequest requestBean;
	private AccountRecordAdapter adapter;
	private ArrayList<ResponseAccountRecord> list;
	private int page;
	private XListView xListView;
	private RelativeLayout rel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account);

	}

	/**
	 * 0收支明细，取消退款，重新拉去数据
	 */
	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_AccountRecordList.REFRESH:
			page = 1;
			getData();
			break;

		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	public void initView() {

		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	
	}

	@Override
	public void initData() {

		setMyTitle("收支明细");
		page = 1;
		requestBean = new Get_user_account_recordRequest();
		requestBean.pagesize = CodeConstant.pageSize;
		list = new ArrayList<ResponseAccountRecord>();
		adapter = new AccountRecordAdapter(list, this, R.layout.item_szmx);
		adapter.setIn(new ListViewOnclickInterFace() {

			@Override
			public Object DoSomeThing(Object data) {
				Intent intent;
				ResponseAccountRecord bean = (ResponseAccountRecord) data;
				if (bean.is_success == 1 && bean.process_type == 2) {
					intent = new Intent(MyAppcation.getMyAppcation(), AccountRecordDetailRefundActivity.class);
				} else {
					intent = new Intent(MyAppcation.getMyAppcation(), AccountRecordDetailActivity.class);
				}
				intent.putExtra("bean", bean);
				startActivity(intent);
				return null;
			}
		});
		xListView.setAdapter(adapter);
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		promptDialog.show();
		getData();
	}

	private void getData() {

		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
				Get_user_account_recordResponse base = JSON
						.parseObject(response, Get_user_account_recordResponse.class);
				if (page == 1)
					list.clear();
				list.addAll(base.list);
				if (base.list == null || base.list.size() == 0) {
					xListView.setPullLoadEnable(false);
				}
				if (list.size() > 0) {
					xListView.setVisibility(View.VISIBLE);
					rel.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void initEvent() {

		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				page = 1;
				xListView.setPullLoadEnable(true);
				getData();
			}

			@Override
			public void onLoadMore() {
				page++;
				getData();
			}
		});
	}

}
